package com.awspaas.user.apps.processforsap.event;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListener;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListenerInterface;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.cc.RDSAPI;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.sofa.common.profile.StringUtil;
import com.awspaas.user.apps.processforsap.util.HttpClientGwUtil;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class GWTaskValidateForGWNOFORM extends InterruptListener implements InterruptListenerInterface {
    @Override
    public boolean execute(ProcessExecutionContext ctx) throws Exception {
        /**
         * 电子
         */
        boolean isty = ctx.isChoiceActionMenu("同意");
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (isty) {

            String rdsId = SDK.getRuleAPI().executeAtScript("@getSapParamas(SAP_MID_ID)");
            RDSAPI rdsapi = SDK.getCCAPI().getRDSAPI(rdsId);
            //取值
            BO bo = ctx.getBO("BO_EU_SAP_WLZSJ");

            List<BO> list = SDK.getBOAPI().query("BO_EU_SAP_JBST").bindId(bo.getBindId()).list();

            String matlType2= list.get(0).getString("MATL_TYPE");

            String isBg = bo.getString("IS_BG");

            //物料特性校验卡控






            //获取物料特性
            BO bo1 = SDK.getBOAPI().query("BO_EU_SAP_CPWLL").detailByBindId(bo.getBindId());
            String zgyunit = "";
            if (bo1 != null) {
                if (StringUtil.isNotEmpty(bo1.getString("ZGYUNIT"))) {
                    zgyunit = bo1.getString("ZGYUNIT");
                }
            }

            for (BO bo11 : list) {
                String[] type = {"FH00", "FS00"};
                Set<String> sh = new HashSet<>();
                 if (isBg.equals("1")) {
                    sh.add("1");
                }
                for (String bukr : type) {
                    for (String sh1 : sh) {
                        if (bukr.startsWith("FH") || bukr.startsWith("FS")) {
                            String tokenUrl = "http://10.21.89.45:8099/pms/api/session";
                            JSONObject jsonObject = new JSONObject();
                            jsonObject.put("userno", bukr.startsWith("FH") ? "3312964743-ERP" : "33129661F0-ERP");
                            jsonObject.put("password", bukr.startsWith("FH") ? "zhgw@123" : "zhgw@123");
                            jsonObject.put("isVerify", true);
                            String result = HttpClientGwUtil.doPost(tokenUrl, jsonObject.toJSONString());
                            System.out.println("------------result:"+result);
                            JSONObject jsonResult = JSONObject.parseObject(result);
                            if (jsonResult.getBoolean("success")) {
                                JSONObject data = jsonResult.getJSONObject("data");
                                String token = data.getString("accessToken");
                                //开始调用业务代码
                                String url = "http://10.21.89.45:8099/gw-edi-up/api/open/v1/erpMat/insertErpMat";
                                JSONObject allData = new JSONObject();
                                JSONArray dataInfo = new JSONArray();
                                JSONObject jsonObject1 = new JSONObject();
                                jsonObject1.put("bondMark", sh1);
                                jsonObject1.put("gMark", bo11.getString("MATL_TYPE").equals("FERT") ? "E" : "I");
                                jsonObject1.put("facGNo", bo11.getString("MATERIAL"));
                                String matlDesc = bo11.getString("SPMC");
                                jsonObject1.put("copGName", matlDesc);
                                jsonObject1.put("copGModelEn", bo11.getString("BASIC_MATL"));
                                jsonObject1.put("note", bo11.getString("APPLICABLEMACHINE"));
                                jsonObject1.put("specifiCations", bo11.getString("GG"));
                                jsonObject1.put("createDepartment", "采购处");
                                jsonObject1.put("lastUpdateTime", simpleDateFormat.format(date));
                                String name = SDK.getDictAPI().getValue("com.awspaas.user.apps.processforsap", "JLDWFORGW", zgyunit);

                                jsonObject1.put("unitErp", name);

                                String sql = "select * from WFH_TASK WHERE (ACTIVITYDEFID = 'obj_cb037016391000018e8ca3d0205744d0' OR ACTIVITYDEFID='obj_cb037016391000018e8ca3d0205744d0' OR ACTIVITYDEFID='obj_cb23f4def5300001e14e2355493115c5') AND PROCESSINSTID = '"+bo.getBindId()+"'";
                                RowMap rowMap = DBSql.getMap(sql);
                                if (rowMap!=null){
                                    String target= rowMap.getString("TARGET");
                                    jsonObject1.put("makeInsert", SDK.getORGAPI().getUser(target).getUserName()+target);
                                }

                                dataInfo.add(jsonObject1);
                                allData.put("data", dataInfo);
                                String result1 = HttpClientGwUtil.doPost1(url, allData.toJSONString(), token);

                            } else {
                                throw new BPMNError("0331", "意外的接口调用错误！");
                            }
                        }
                    }
                }
            }
        }
        return true;
    }
}
